package com.lib.basex.widget.border;

import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;

import com.lib.basex.R;

public class LViewHelper implements IShapeView {

    private LViewStyle normalStyle;
    private LViewStyle pressStyle;
    private LViewStyle unEnableStyle;
    private final View mView;

    public LViewHelper(@NonNull View mView, AttributeSet attrs) {
        this.mView = mView;
        if (null != attrs) {
            TypedArray array = mView.getContext().obtainStyledAttributes(attrs, R.styleable.LStyleView);
            int color = array.getColor(R.styleable.LStyleView_ls_color, Color.TRANSPARENT);
            int borderColor = array.getColor(R.styleable.LStyleView_ls_border_color, Color.TRANSPARENT);
            int width = array.getDimensionPixelSize(R.styleable.LStyleView_ls_border_width, 0);
            int direct = array.getInt(R.styleable.LStyleView_ls_border_direct, LViewStyle.LINE_ALL);
            int radius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_radius, 0);
            int lbRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_left_bottom_radius, 0);
            int ltRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_left_top_radius, 0);
            int rbRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_right_bottom_radius, 0);
            int rtRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_right_top_radius, 0);

            normalStyle = new LViewStyle(color, borderColor, width, direct, radius, lbRadius, ltRadius, rbRadius, rtRadius);

            int p_color = array.getColor(R.styleable.LStyleView_ls_press_color, normalStyle.color);
            int p_borderColor = array.getColor(R.styleable.LStyleView_ls_press_border_color, normalStyle.strokeColor);
            int p_width = array.getDimensionPixelSize(R.styleable.LStyleView_ls_press_border_width, normalStyle.width);
            int p_direct = array.getInt(R.styleable.LStyleView_ls_press_border_direct, normalStyle.direct);
            int p_radius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_press_radius, normalStyle.radius);
            int p_lbRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_press_left_bottom_radius, normalStyle.leftBottomRadius);
            int p_ltRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_press_left_top_radius, normalStyle.leftTopRadius);
            int p_rbRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_press_right_bottom_radius, normalStyle.rightBottomRadius);
            int p_rtRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_press_right_top_radius, normalStyle.rightTopRadius);
            pressStyle = new LViewStyle(p_color, p_borderColor, p_width, p_direct, p_radius, p_lbRadius, p_ltRadius, p_rbRadius, p_rtRadius);


            int un_color = array.getColor(R.styleable.LStyleView_ls_un_enable_color, normalStyle.color);
            int un_borderColor = array.getColor(R.styleable.LStyleView_ls_un_enable_border_color, normalStyle.strokeColor);
            int un_width = array.getDimensionPixelSize(R.styleable.LStyleView_ls_un_enable_border_width, normalStyle.width);
            int un_direct = array.getInt(R.styleable.LStyleView_ls_un_enable_border_direct, normalStyle.direct);
            int un_radius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_un_enable_radius, normalStyle.radius);
            int un_lbRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_un_enable_left_bottom_radius, normalStyle.leftBottomRadius);
            int un_ltRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_un_enable_left_top_radius, normalStyle.leftTopRadius);
            int un_rbRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_un_enable_right_bottom_radius, normalStyle.rightBottomRadius);
            int un_rtRadius = array.getDimensionPixelSize(R.styleable.LStyleView_ls_un_enable_right_top_radius, normalStyle.rightTopRadius);
            unEnableStyle = new LViewStyle(un_color, un_borderColor, un_width, un_direct, un_radius, un_lbRadius, un_ltRadius, un_rbRadius, un_rtRadius);
            update();
            array.recycle();
        }

    }

    @Override
    public void update() {
        if (normalStyle == null) {
            return;
        }
        if (pressStyle == null) {
            pressStyle = normalStyle.copy();
        }
        if (unEnableStyle == null) {
            unEnableStyle = normalStyle.copy();
        }
        if (normalStyle.equals(pressStyle) && normalStyle.equals(unEnableStyle)) {
            mView.setBackgroundDrawable(normalStyle.toDrawable());
            return;
        }

        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(new int[]{android.R.attr.state_pressed}, pressStyle.toDrawable());
        drawable.addState(new int[]{-android.R.attr.state_enabled}, unEnableStyle.toDrawable());
        drawable.addState(new int[]{-android.R.attr.state_pressed, android.R.attr.state_enabled}, normalStyle.toDrawable());
        mView.setBackgroundDrawable(drawable);
    }

    @Override
    public LViewStyle getNormalStyle() {
        return normalStyle;
    }

    @Override
    public LViewStyle getPressStyle() {
        return pressStyle;
    }


    @Override
    public LViewStyle getUnEnableStyle() {
        return unEnableStyle;
    }

    @Override
    public void setUnEnableStyle(LViewStyle unEnableStyle) {
        this.unEnableStyle = unEnableStyle;
    }

    @Override
    public void setNormalStyle(LViewStyle normalStyle) {
        this.normalStyle = normalStyle;
    }

    @Override
    public void setPressStyle(LViewStyle pressStyle) {
        this.pressStyle = pressStyle;
    }
}
